<template>
  <div>
    <!-- 查询参数及按钮部分 -->
    <el-dialog  v-dialogDrag  
      :close-on-click-modal="false"
      :title="dialogStatus"
      :visible.sync="dialogEditFormVisible"
    >
      <el-form
        ref="dataForm"
        class="dialog-Backgroundcolor"
        :model="listQuery"
        label-position="right"
        label-width="100px"
      >
        <el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="开户银行：">
                <el-select
                  v-model="listQuery.salaCardBank"
                  class="filter-item"
                  clearable
                  placeholder="--请选择--"
                >
                  <el-option
                    v-for="item in agencyBankLists"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="身份证号码：">
                <el-input
                  v-model="listQuery.idenNo"
                  class="filter-item Borderwidth"
                />
              </el-form-item>
            </el-col>

          </el-row>
          <el-button
            type="primary"
            style="float:right; margin-top:10px"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      fit
      :height="tableHeight"
      row-key="id"
      :tree-props="{children: 'children'}"
      style="width: 100%;"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        prop="no"
        label="序号"
        align="center"
        width="80"
      />
      <el-table-column
        label="单位名称"
        align="center"
      >
        <template slot-scope="{row}">
          <span style="float: left">{{ row.agencyName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="personName"
        label="姓名"
        align="center"
      />
      <el-table-column
        prop="idenNo"
        width="170"
        label="身份证号码"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.idenNo  }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="原开户行"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.salaCardBank | bankFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="oldSalaCardNo"
        width="170"
        label="原工资卡号"
        align="center"
      />
      <el-table-column
        prop="bankBackRemark"
        label="账户状态"
        align="center"
      />
      <el-table-column
        label="回盘时间"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getSex, getAgencyBank } from '@/api/baseInfo'
import {
  querySalaryBankPayBackPage,
  sendCardNoToAgency,
  cancelSendCard
} from '@/api/exceptionCardNumberManage/exceptionCardNumberSend'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  name: 'ExceptionCardNum',
  components: { Pagination, tableEmpty },
  filters: {
    bankFilter(type, that) {
      const agencyBankLists = that.agencyBankLists
      for (const item in agencyBankLists) {
        if (agencyBankLists[item].code === type) {
          return agencyBankLists[item].name
        }
      }
      return ''
    },
    sexFilter(type, that) {
      const sexLists = that.sexLists
      for (const item in sexLists) {
        if (sexLists[item].code === type) {
          return sexLists[item].name
        }
      }
      return ''
    },
    //号码脱敏显示
    hideMiddle(val) {
      if(val!== undefined&& val!== null){//避免列表中存在空数据报错
        return `${val.substring(0,4)}****${val.substring(val.length-4)}`
      }else{
        return val
      }
    }
  },
  props: ['tabPane'],
  data() {
    return {
      tableHeight: null,
      dialogStatus: '',
      that: this,
      dialogEditFormVisible: false,
      listLoading: true,
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 200,
        flag: '0',
        salaCardBank: '',
        idenNo: '',
        agencyId: '0'
      },
      sexLists: [],
      agencyBankLists: [],
      operatePersonLists: []
    }
  },
  watch: {
    list(val) {
      this.doLayout()
    }
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  // 开始的地方
  created() {
    this.listQuery.flag = this.tabPane
    this.initBaseInfo()
    this.getList()
  },
  methods: {
    // 树方法
    handleTree(data) {
      // if (!Object.prototype.hasOwnProperty.call(data, 'children')) {
      this.listQuery.agencyId = data.id
      this.getList()
      // }
    },
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    getTbHeight() {
      this.tableHeight = window.innerHeight - 220
    },
    // 查询
    getList() {
      this.listLoading = true
      querySalaryBankPayBackPage(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleQuery() {
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
      this.dialogEditFormVisible = false
    },
    // 查询
    query() {
      this.dialogStatus = '查询'
      this.dialogEditFormVisible = true
    },
    // 分发到单位
    sendCardNoToAgency() {
      this.operatePersonLists = []
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要分发的单位',
          type: 'warning',
          duration: 4000
        })
        this.$emit('sendCardNoCallBack')
        return
      }
      selection.forEach(item => {
        this.operatePersonLists.push(item.agencyId)
      })
      // this.$confirm('确定分发吗?', '提示', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      sendCardNoToAgency(this.operatePersonLists).then(response => {
        this.$message({
          showClose: true,
          message: '分发成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('sendCardNoCallBack')
        this.handleQuery()
        // })
      })
        .catch(() => {
          this.$emit('sendCardNoCallBack')
        })
    },
    // 重新下发
    cancelSendCard() {
      this.operatePersonLists = []
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择要重新下发的单位',
          type: 'warning',
          duration: 4000
        })
        this.$emit('cancelSendCallBack')
        return
      }
      selection.forEach(item => {
        this.operatePersonLists.push(item.agencyId)
      })
      // this.$confirm('确定重新下发吗?', '提示', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      cancelSendCard(this.operatePersonLists).then(response => {
        if (response.data === '500') {
          this.$message({
            showClose: true,
            message: '存在财政已确认的单位，无法重新下发',
            type: 'warning',
            duration: 4000
          })
          this.$emit('cancelSendCallBack')
        } else if (response.data === '200') {
          this.$message({
            showClose: true,
            message: '重新下发成功',
            type: 'success',
            duration: 4000
          })
          this.$emit('cancelSendCallBack')
        }
        this.handleQuery()
        // })
      })
        .catch(() => {
          this.$emit('cancelSendCallBack')
        })
    },
    // 初始化基础数据
    initBaseInfo() {
      getSex().then(response => {
        this.sexLists = Object.assign({}, response.data)
      })
      getAgencyBank().then(response => {
        this.agencyBankLists = Object.assign({}, response.data)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__row--level-1 {
  .el-table-column--selection {
    .el-checkbox {
      display: none;
    }
  }
}
</style>

